<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


include "header.php";
include "/library/receipt.php";

$page = 0;
$perpage=30;
$sort="";
$order ="";

if(isset($_GET['search'])){
                $order= $order . " WHERE customer_name LIKE '%".$_GET['search']."%' OR id LIKE '%".receipt_id_check_out($_GET['search'])."%' ";
}
if(isset($_GET['sort'])){
                if($_GET['sort']==0) $sort = "id";
                else if($_GET['sort']==1) $sort = "customer_name";
                else if($_GET['sort']==2) $sort = "date";
                $order =$order . "ORDER BY ".$sort;
}else
    $order=$order . "ORDER BY id";
$temp = showAll($order);
$allContent =mysql_num_rows($temp);
if(isset($_GET['page'])){
                $page =  $_GET['page'] ;

                $start = $page * $perpage;

                $order = $order . " LIMIT ".$start.", ".$perpage;

}else
                $order = $order . " LIMIT 0, ".$perpage;



?>
<script type="text/javascript">
    $(document).ready(function(){
        $('#sortSelect').change(function(){
            $('#receiptForm').submit();
        });
    });
    


</script>


<div id="receiptOrder" >
    <form action="receipt.php" method="get" id="receiptForm">
    เรียงตาม
    
    <select name="sort" id="sortSelect">

        <option value="0" <?php if($sort=='id') echo "selected=selected";?>>เลขที่ใบเสร็จ</option>

        <option value="1" <?php if($sort=='customer_name') echo "selected=selected";?>>ชื่อลูกค้า</option>

        <option value="2" <?php if($sort=='date') echo "selected=selected";?>>วันที่</option>

    </select>
    </form>
</div>
<div id="searchDiv">
    <form action="receipt.php" method="get">
        <input type="text" name="search" size="80" value="<?php if(isset($_GET['search'])) { echo $_GET['search'];  } ?>">
        <input type="submit" value="ค้นหา">
        <?php if(isset($_GET['sort'])) { ?> <input type="hidden" name="sort" value="<?=$_GET['sort']?>"> <?php } ?>
    </form>
</div>
<div id="pageNumber">
    หน้า 
    <?php

    $allPage = ceil($allContent/$perpage) ;

    for($i = 1 ; $i <= $allPage ;$i++){
        $te=$i-1;
        $opt ="";
        if(isset($_GET['sort']))
            $opt = "&sort=".$_GET['sort'];
        if(isset($_GET['search']))
            $opt = $opt . "&search=".$_GET['search'];
        if($page==$te)
            echo $i;
        else
            echo "<a href='receipt.php?page=$te$opt'>$i </a>";
    }
    ?>
</div>

<div name="multivalue" align="center" width="100%" >
    <table  id="tableReceipt">
        <tr>
            <th>เลขที่ใบเสร็จ</th>
            <th >วันที่</th>            
            <th >ประเภทใบเสร็จ</th>
            <th >ชื่อลูกค้า</th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
        <?php

            if(isset($_GET['sort'])){
                if($_GET['sort']==0) $sort = "id";
                else if($_GET['sort']==1) $sort = "customer_name";
                else if($_GET['sort']==2) $sort = "date";
                
            }
            if(isset($_GET))
                $output = showAll($order);
            else $output = showAll('id');
            while ($print = mysql_fetch_array($output)){
                

                ?>

        <tr>
            <td><?= receipt_id_check($print['id']); ?></td>
            <td ><?=  sql_to_date($print['date']); ?></td>            
            <td ><?= receipt_type($print['receipt_type']); ?></td>
            <td ><?=$print['customer_name']; ?></td>
            
            
            <td><a href="receipt_add.php?editform=&id=<?=$print['id'];?>"><input type="button" value="แก้ไข"></a></td>
            <td><a href="receipt_add.php?delete=&id=<?=$print['id'];?>"><input type="button" value="ลบ"></a></td>
            <td><a href="export_pdf.php?id=<?=$print['id'];?>"><input type="button" value="ปริ้น"></a></td>
        </tr>
        <?php }  ?>
    </table>

    <p>
    </p>

</div>
